package is.web.table;

import is.NullUtil;
import is.interfaces.Dao;
import is.interfaces.Table;
import is.model.bean.Department;
import is.model.bean.Employee;
import is.model.dao.DepartmentDao;
import is.model.exceptions.EmptyValueException;
import is.web.GetParam;
import java.sql.SQLException;

/**
 *
 * @author aspr0
 */
public class EmployeeTable extends AbstractTable<Employee> implements Table<Employee>
{
    @Override
    public String genTable() throws EmptyValueException, SQLException
    {

        String table =
                "<table>"
                + "<thead>%s</thead>"
                + "<tbody>%s%s</tbody>"
                + "</table>";
        String header =
                "<tr>"
                + "<td><a href=\".\">id</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=0\">last name</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=1\">first name</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=2\">middle name</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=3\">department</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=4\">telephone</a> %s</td>"
                + "<td><a href=\"?" + GetParam.ORDER_BY + "=5\">salary</a> %s</td>"
                + "<td>tools</td>"
                + "</tr>";
        String id = (hasSearched) ? NullUtil.toString(initialBean.getId()) : "";
        String lastName = (hasSearched) ? NullUtil.toString(initialBean.getLastName()) : "";
        String firstName = (hasSearched) ? NullUtil.toString(initialBean.getFirstName()) : "";
        String middleName = (hasSearched) ? NullUtil.toString(initialBean.getMiddleName()) : "";
        String telephone = (hasSearched) ? NullUtil.toString(initialBean.getTelephone()) : "";
        String salary = (hasSearched) ? NullUtil.toString(initialBean.getSalary()) : "";
        String searchForm =
                "<tr>"
                + "<form method=\"post\">"
                + "<td><input type=\"text\" name=\"id\" value=\"" + id + "\" class=\"search-id\"></td>"
                + "<td><input type=\"text\" name=\"lastName\" value=\"" + lastName + "\" class=\"search-wide\"></td>"
                + "<td><input type=\"text\" name=\"firstName\" value=\"" + firstName + "\" class=\"search-wide\"></td>"
                + "<td><input type=\"text\" name=\"middleName\" value=\"" + middleName + "\" class=\"search-wide\"></td>"
                + "<td></td>"
                + "<td><input type=\"text\" name=\"telephone\" value=\"" + telephone + "\" class=\"search-wide\"></td>"
                + "<td><input type=\"text\" name=\"salary\" value=\"" + salary + "\" class=\"search-wide\"></td>"
                + "<td><input type=\"submit\" value=\"search\"></td>"
                + "</form>"
                + "</tr>";
        String body = "";

        switch (orderBy)
        {
            case 0:
                header = String.format(header, "", ARROW_UP, "", "", "", "", "");
                break;

            case 1:
                header = String.format(header, "", "", ARROW_UP, "", "", "", "");
                break;

            case 2:
                header = String.format(header, "", "", "", ARROW_UP, "", "", "");
                break;

            case 3:
                header = String.format(header, "", "", "", "", ARROW_UP + "i" , "", "");
                break;

            case 4:
                header = String.format(header, "", "", "", "", "", ARROW_UP, "");
                break;

            case 5:
                header = String.format(header, "", "", "", "", "",  "", ARROW_UP);
                break;

            default:
                header = String.format(header, ARROW_UP, "", "", "", "", "", "", "");
                break;
        }

        for (Employee employee : beans)
        {
            Department initialDepartment = new Department();
            initialDepartment.setId(employee.getDepartmentId());
            Dao<Department> departmentDao = new DepartmentDao();
            departmentDao.setInitialBean(initialDepartment);
            Department department = departmentDao.retrieve();

            String departmentLink = NullUtil.toLink("<a href=\"../department/retrieve.jsp?id=%s\">%s</a>", department.getId(), department.getName());


            String row =
                    "<tr>"
                    + "<td>" + employee.getId() + "</td>"
                    + "<td>" + employee.getLastName() + "</td>"
                    + "<td>" + employee.getFirstName() + "</td>"
                    + "<td>" + employee.getMiddleName() + "</td>"
                    + "<td>" + departmentLink + "</td>"
                    + "<td>" + employee.getTelephone() + "</td>"
                    + "<td>" + employee.getSalary() + "</td>"
                    + "<td>"
                    + "<a href=\"retrieve.jsp?id=" + employee.getId() + "\" title=\"retrieve\">"
                    + "<image src=\"../img/document-open.png\" class=\"hMargin\"></a>"
                    + "<a href=\"update.jsp?id=" + employee.getId() + "\" title=\"update\">"
                    + "<image src=\"../img/document-properties.png\" class=\"hMargin\"></a>"
                    + "<a href=\"copy.jsp?id=" + employee.getId() + "\" title=\"copy\">"
                    + "<image src=\"../img/edit-copy.png\" class=\"hMargin\"></a>"
                    + "<a href=\"delete.jsp?id=" + employee.getId() + "\" title=\"delete\">"
                    + "<image src=\"../img/edit-delete.png\" class=\"hMargin\"></a>"
                    + "</td>"
                    + "</tr>";
            body += row;
        }
        return String.format(table, header, searchForm, body);
    }
}
